package cn.yitun.chat.yitunservermanagecore.general.manage.index.controller;

import cn.yitun.chat.yituncommonmessage.ExceptionEnum;
import cn.yitun.chat.yituncommonmessage.Info;
import cn.yitun.chat.yituncommonmessage.YitunException;
import cn.yitun.chat.yituncommonmessage.result.ResultMessage;
import cn.yitun.chat.yituncommonutil.YitunCollectionUtil;
import cn.yitun.chat.yituncommonutil.YitunStringUtil;
import cn.yitun.chat.yitungeneral.annotation.action.MethodMapping;
import cn.yitun.chat.yitungeneral.annotation.codec.Encryped;
import cn.yitun.chat.yitungeneral.annotation.parameter.JsonDefine;
import cn.yitun.chat.yitunnetsession.session.SocketSession;
import cn.yitun.chat.yitunservercommonlib.converter.dto2vo.user.UserDto2VoConverter;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.system.user.UserAccountDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.system.user.UserDto;
import cn.yitun.chat.yitunservermanagecore.general.manage.index.service.AccountService;
import cn.yitun.chat.yitunservermanagecore.general.manage.index.service.SecurityQuestionService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/manage/account/")
public class AccountController {


    @Resource
    private SecurityQuestionService securityQuestionService;
    @Resource
    private AccountService accountService;

    /**
     *
     * Date 2019-01-20 08:39:39<br>
     * Description 用户注册
     *
     * @param user ：用户信息
     * user.getSecurityQuestionList()：密保问列表
     * @return
     * @author yitun<br>
     * @version 1.1
     */
    @MethodMapping(value = "", isIntercept = false)
    @RequestMapping(value = "/register")
    @Encryped(decode = true)
    public Object register(
            @JsonDefine("body") UserDto user) {
        if ( YitunStringUtil.isEmpty(user.getAccount()) || YitunStringUtil.isEmpty(user.getPassword())
                || YitunStringUtil.isEmpty(user.getNickname())
                || YitunStringUtil.isEmpty(user.getName())
                || YitunStringUtil.isEmpty(user.getMobile())
                || YitunStringUtil.isEmpty(user.getEmail())
                || YitunCollectionUtil.isEmpty(user.getSecurityQuestionList()))
            throw new YitunException(ExceptionEnum.LOSE_FILED);

        ResultMessage message = new ResultMessage();
        try {
            Info info = accountService.register(user, user.getSecurityQuestionList());
            message.bodyPut("user", UserDto2VoConverter.INSTANCE.dto2vo(user));
            message.setInfo(info);
        } catch (Exception e) {
            e.printStackTrace();
            message.addError(ExceptionEnum.BUSY_SERVER);
            message.addWarning(ExceptionEnum.REGISTER_FAIL);
        }
        return message;
    }



    /**
     *
     * Description 获取密保问题 <br>
     * Date 2019-04-20 17:33:25<br>
     *
     * @param account
     * @return
     * @since 1.1
     */
    @MethodMapping(value = "", isIntercept = false)
    @PostMapping(value = "/security_question_list")
    public Object getSecurityQuestionList(@JsonDefine("body") UserAccountDto account) {
        ResultMessage message = securityQuestionService.getSecurityQuestionDataListByAccount(account.getAccount());
        return message;
    }

    /**
     *
     * Description <br>
     * Date 2019-04-27 09:05:50<br>
     *
     * @param accountDto
     * @return Object
     * @since 1.1
     */
    @MethodMapping(value = "", isIntercept = false)
    @PostMapping(value = "/update_pwd")
    @Encryped(decode = true)
    public Object updatePassword(
            @JsonDefine("body") UserAccountDto accountDto) {
        if (accountDto.getUserId() == null || YitunStringUtil.isEmpty(accountDto.getPassword()))
            throw new YitunException(ExceptionEnum.LOSE_FILED);

        Info message = securityQuestionService.updatePassword(accountDto);
        return message;
    }

    /**
     *
     * Description 账号是否存在 <br>
     * Date 2019-04-28 00:23:46<br>
     *
     * @param accountDto
     * @return Object
     * @since 1.1
     */
    @MethodMapping(value = "", isIntercept = false)
    @PostMapping(value = "/is_exist_account")
    public Object isExistAccount(
            SocketSession socketSession,
            @JsonDefine("body") UserAccountDto accountDto) {
        if (accountDto.getUserId() == null && YitunStringUtil.isEmpty(accountDto.getAccount()))
            throw new YitunException(ExceptionEnum.LOSE_FILED);

        Long userId = accountDto.getUserId();
        String account = accountDto.getAccount();
//		userId = socketSession.getKey();
        boolean exist = accountService.isExistAccount(userId, account);
        ResultMessage message = new ResultMessage();
        message.bodyPut("exist", exist);
        return message;
    }

    /**
     *
     * Description 邮箱是否已经存在 <br>
     * Date 2019-04-28 00:27:49<br>
     *
     * @param accountDto
     * @return Object
     * @since 1.1
     */
    @MethodMapping(value = "", isIntercept = false)
    @PostMapping(value = "/is_exist_email")
    public Object isExistEmail(
            SocketSession socketSession,
            @JsonDefine("body") UserAccountDto accountDto) {
        if (accountDto.getUserId() == null && YitunStringUtil.isEmpty(accountDto.getEmail()))
            throw new YitunException(ExceptionEnum.LOSE_FILED);
//		userId = socketSession.getKey() ;
        boolean exist = accountService.isExistEmail(accountDto.getUserId(), accountDto.getEmail());
        ResultMessage message = new ResultMessage();
        message.bodyPut("exist", exist);
        return message;
    }

    /**
     *
     * Description 手机号码是否存在 <br>
     * Date 2019-04-28 00:29:11<br>
     *
     * @param accountDto
     * @return Object
     * @since 1.0.0
     */
    @MethodMapping(value = "", isIntercept = false)
    @PostMapping(value = "/is_exist_mobile")
    public Object isExistMobile(
            SocketSession socketSession,
            @JsonDefine("body") UserAccountDto accountDto) {
//		userId = socketSession.getKey();
        boolean exist = accountService.isExistMobile(accountDto.getUserId(), accountDto.getMobile());
        ResultMessage message = new ResultMessage();
        message.bodyPut("exist", exist);
        return message;
    }
}
